package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.domain.TbAddFriend;
import com.bwie.domain.TbFriend;
import com.bwie.mapper.TbFriendMapper;
import com.bwie.param.TbAddFriendParam;
import com.bwie.service.TbAddFriendService;
import com.bwie.mapper.TbAddFriendMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author xinghe
* @description 针对表【tb_add_friend(所有用户)】的数据库操作Service实现
* @createDate 2025-06-17 13:55:38
*/
@Service
public class TbAddFriendServiceImpl extends ServiceImpl<TbAddFriendMapper, TbAddFriend>
    implements TbAddFriendService{

    @Autowired
    private TbAddFriendMapper tbAddFriendMapper;

    @Autowired
    private TbFriendMapper tbFriendMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public R addList(TbAddFriendParam tbAddFriendParam) {

        Page<TbAddFriend> page = new Page<>(tbAddFriendParam.getPage(), tbAddFriendParam.getLimit());
        Page<TbAddFriend> tbAddFriendPage =null;
        if(tbAddFriendParam.getNum() != null && tbAddFriendParam.getNum() == 1){
            tbAddFriendPage = tbAddFriendMapper.selectAlldPage(page, tbAddFriendParam);
        }else if(tbAddFriendParam.getNum() != null && tbAddFriendParam.getNum() == 1){
            tbAddFriendPage = tbAddFriendMapper.selectAlldPage1(page, tbAddFriendParam);
        }else{
            tbAddFriendPage = tbAddFriendMapper.selectAllPage(page, tbAddFriendParam);
        }


        return R.OK(tbAddFriendPage);

    }

    @Override
    @Transactional
    public R add(TbAddFriendParam tbAddFriendParam) {

        TbAddFriend tbAddFriend = tbAddFriendMapper.selectOne(
                new QueryWrapper<TbAddFriend>().lambda().eq(TbAddFriend::getId, tbAddFriendParam.getId())
        );

        tbAddFriend.setUserStatus(tbAddFriendParam.getUserId());

        tbAddFriendMapper.updateById(tbAddFriend);

        TbFriend tbFriend = new TbFriend();
        tbFriend.setFriendName(tbAddFriend.getName());
        tbFriend.setSex(tbAddFriend.getSex());
        tbFriend.setPhone(tbAddFriend.getPhone());
        tbFriend.setAddress(tbAddFriend.getAddress());
        tbFriend.setBirthday(tbAddFriend.getBirthday());
        tbFriend.setStatus(tbAddFriend.getStatus());
        tbFriend.setUserId(tbAddFriendParam.getUserId());
        tbFriendMapper.insert(tbFriend);

        String key = "tbFriend-" + tbAddFriendParam.getUserId();

        stringRedisTemplate.delete(key);

        List<TbFriend> list = tbFriendMapper.selectByUserId(tbAddFriendParam.getUserId());

        stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(list),60, TimeUnit.MINUTES);

        return R.OK();
    }
}




